package app

import (
	"errors"
	"github.com/gin-gonic/gin"
	"sanrenx.com/xyxf/pkg/e"
)

type Gin struct {
	C *gin.Context
}

type Response struct {
	Code int         `json:"code"`
	Msg  string      `json:"msg"`
	Data interface{} `json:"data"`
}

// Response setting gin.JSON
func (g *Gin) Response(httpCode, errCode int, data interface{}) {
	g.C.JSON(httpCode, Response{
		Code: errCode,
		Msg:  e.GetMsg(errCode),
		Data: data,
	})
	return
}

func (g *Gin) GetUID() (uint64, error) {
	if value, exists := g.C.Get("uid"); exists {
		return value.(uint64), nil
	}

	return 0, errors.New("未登录")
}

func (g *Gin) GetPhone() (string, error) {
	if value, exists := g.C.Get("phone"); exists {
		return value.(string), nil
	}
	return "", errors.New("未登录")
}

func (g *Gin) GetRole() (string, error) {
	if value, exists := g.C.Get("role"); exists {
		return value.(string), nil
	}

	return "", errors.New("未授权")
}
